/*
 *  ClientDispatchLog.c
 *  ClientDispatchTest
 *
 *  Created by Andrey on 04/11/2008.
 *  Copyright 2008 ИУ7-2003. All rights reserved.
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include "ClientDispatchLog.h"
#include "../TLog/tlog.h"

struct tlog_instance* _tLog;
int _logFileDescriptor;
char _messageBuffer[MAX_LOG_MESSAGE_LENGTH];

int InitClientDispatchLog(const char* logFilename)
{
	int returnValue = 0;
	struct tlog_options logOptions;
	logOptions.max_message_size = MAX_LOG_MESSAGE_LENGTH;
	logOptions.log_queue_size = 32;
	
	_logFileDescriptor = open(logFilename, O_APPEND | O_RDWR, S_IRWXU);
	//_tLog = tlog_init(_logFileDescriptor, &logOptions);
	_tLog = tlog_init(STDOUT_FILENO, &logOptions);
	
	return returnValue;
}

void Log(const char* format, ...)
{
	va_list argListPtr;
	
	va_start(argListPtr, format);
	vsnprintf(_messageBuffer, MAX_LOG_MESSAGE_LENGTH, format, argListPtr);
	va_end(argListPtr);
	
	tlog_log(_tLog, LL_DEBUG, _messageBuffer);
}

void ShutdownClientDispatchLog()
{
	tlog_cleanup(_tLog);
	close(_logFileDescriptor);
}